﻿@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IHttpService _httpService
@inject IJSRuntime JS
@inject I18n I18n
@inject ILocalStorageService _localStorage
@inject IEventService _eventService
@inject IServiceProvider _serviceProvider
@inject IMauiService _mauiService

@using BlazorComponent.I18n
@using CnGalWebSite.Core.Models;
@using CnGalWebSite.Shared.Layout;



<MApp>
    <CascadingValue Value="connectionInfo" IsFixed="true">
        <CascadingAuthenticationState>
            <CnGalWebSite.Shared.Components.CnGalRootTip>
                <Router AppAssembly="@typeof(App).Assembly" PreferExactMatches="@true">
                    <Found Context="routeData">
                        @if (ToolHelper.IsSSR)
                        {
                            <RouteView RouteData="@routeData" DefaultLayout="LayoutType" />
                        }
                        else
                        {
                            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="LayoutType">
                                <NotAuthorized>
                                    <NotAuthorizedTipView NoPermissions="@(context.User.Identity?.IsAuthenticated == true)" />
                                </NotAuthorized>
                                <Authorizing>

                                </Authorizing>
                            </AuthorizeRouteView>
                        }
                        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
                    </Found>
                    <NotFound>
                        <LayoutView Layout="LayoutType">
                            <NotFoundCard Page />
                         </LayoutView>
                     </NotFound>
                 </Router>
             </CnGalWebSite.Shared.Components.CnGalRootTip>
         </CascadingAuthenticationState>
     </CascadingValue>
 </MApp>


 @code {
    [Parameter]
    public ConnectionInfo? connectionInfo { get; set; }
    [Parameter]
    public bool? Mobile { get; set; }


    private System.Threading.Timer mytimer;
    public Type LayoutType
    {
        get
        {
            return NavigationManager.Uri.Contains("/admin") ? typeof(AdminLayout) : (_dataCacheService.IsApp ? typeof(AppLayout) : typeof(PCLayout));
        }
    }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        if (parameters.TryGetValue<bool?>(nameof(Mobile), out var value))
        {
            //SSR 预置值
            if (value != null)
            {
                _dataCacheService.IsApp = value.Value;
            }
        }

        if (ToolHelper.IsMaui)
        {
            _dataCacheService.IsApp = ToolHelper.IsApp;
        }
        //判断来源
        if (NavigationManager.Uri.Contains("ref=gov"))
        {
            _dataCacheService.IsMiniMode = true;
        }
        //将语言切换成zh-CN
        I18n.SetCulture(System.Globalization.CultureInfo.GetCultureInfo("zh-CN"));

        await base.SetParametersAsync(parameters);
    }

    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();

    }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            var needRefresh = false;
            //检查是否为移动设备
            if (!ToolHelper.IsSSR && !ToolHelper.IsMaui)
            {
                _dataCacheService.IsApp = await JS.InvokeAsync<bool>("isMobile");
                _eventService.OnSavaTheme();
            }
            //检查迷你模式
            try
            {
                if (await _localStorage.GetItemAsync<bool>("IsMiniMode"))
                {
                    if (_dataCacheService.IsMiniMode == false)
                    {
                        _dataCacheService.IsMiniMode = true;
                        needRefresh = true;
                    }
                }
                else
                {
                    if (_dataCacheService.IsMiniMode)
                    {
                        await _localStorage.SetItemAsync<bool>("IsMiniMode", true);
                    }
                }

            }
            catch
            {

            }

            if (needRefresh)
            {
                _eventService.OnToggleMiniMode();
            }
        }
    }
}
